const config = require('../config.js')

const app = getApp()

/**
 * 通过学号获取用户ID
 */
function getUserByStudentId(studentId, callback) {
	wx.request({
		url: config.getUserByStudentId,
		data: {
			studentId: studentId
		},
		method: 'GET',
		success: function (result) {
			callback(result.data.user.userId)
		}
	})
}

/**
 * 撤销发布 
 */
function revokeRelease(informationId, callback) {
	wx.request({
		url: config.revokeInformation +'?informationId='+informationId,
		data: {
			'informationId': informationId
		},
		method: 'POST',
		success: function (result) {
			console.log(result)
			callback()
		}
	})
}

/**
 * 获取所有供需信息
 */
function getAllInformation(self){
  var theApp = getApp()
  wx.request({
    url: config.getAllInfoemation,
    data: {
      'userId': theApp.globalData.userId
    },
    method: 'GET',
    success: function (result) {
      console.log(result)
      self.setData({
        informations: result.data.informations
      })
    }
  })
}

/**
 * 发布信息
 */
function addInformation(title, location, tagId, price, imgUrl, deadline, description) {
  var theApp = getApp()
  var userId = theApp.globalData.userId
  wx.request({
    url: config.addInformation,
    data: {
      'userId': userId,
      'title': title,
      'location': location,
      'tagId': tagId,
      'price': price,
      'imgUrl':imgUrl,
      'deadline': deadline,
      'description': description
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    success: function (res) {
    }
  })
}

/**
 * 申请交易
 */
function addIndent(price, description, informationId, guaranteTime, publisherId) {
  var theApp = getApp()
  var userId = theApp.globalData.userId
  wx.request({
    url: config.addIndent,
    data: {
      'price':price, 
      'description':description, 
      'informationId':informationId, 
      'guaranteTime':guaranteTime, 
      'publisherId':publisherId, 
      'applicationId': theApp.globalData.userId
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    success: function (res) {
    }
  })
}

/**
 * 添加收藏
 */
function addCollect(userId,informationId) {
  wx.request({
    url: config.addCollect,
    data: {
      'userId': userId,
      'informationId': informationId
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    success: function (res) {
    }
  })
}

/**
 * 取消收藏
 */
function removeCollect(userId, informationId) {
  wx.request({
    url: config.removeCollect,
    data: {
      'userId': userId,
      'informationId': informationId
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    success: function (res) {
    }
  })
}

/**
 * 提交投诉
 */
function addComplaint(complaintType,reason,informationId) {
  var theApp = getApp()
  var userId = theApp.globalData.userId
  wx.request({
    url: config.addComplaint,
    data: {
      'type': complaintType,
      'reason':reason,
      'informationId': informationId,
      'userId': theApp.globalData.userId
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    success: function (res) {
    }
  })
}

/**
 * 通过indentId 被评价者Id获得评论
 */
function getComment(indentId, evaluatedId, self) {
	wx.request({
		url: config.getCommentByIndentId,
		data: {
			'indentId': indentId,
			'evaluatedId': evaluatedId
		},
		method: 'GET',
		success: function (result) {
      console.log(result)
			self.setData({
        comment: result.data.comment
      })
		}
	})
}

/**
 * 将订单设为已删除
 */
function deleteIndent(indentId, callback, self) {
	console.log(indentId)
	wx.request({
		url: config.deleteIndent+'?indentId='+indentId,
		data: {
			'indentId': indentId
		},
		method: 'DELETE',
		success: function (result) {
			console.log(result)
			callback(self)
		}
	})
}

/**
 * 获得userId所属的所有trades
 */
function getTrades(userId, self) {
	var trades = [[], [], []]
	wx.request({
		url: config.getAll,
		data: {
			'userId': userId
		},
		method: 'GET',
		success: function (result) {
			if(result.data.status == "success"){
				trades[0] = result.data.release
				trades[1] = result.data.apply
				trades[2] = result.data.history
				self.setData({
					trades: trades
				})
			}
		}
	})
}

/**
 * 通过用户ID获取用户信息
 */
function getUser(self) {
  var theApp = getApp()
  var user = [[]]
  wx.request({
    url: config.getUser,
    data: {
      'userId': theApp.globalData.userId
    },
    method: 'GET',
    success: function (result) {
      if (result.data.status == "success") {
        user[0] = result.data.user
        self.setData({
          user: user[0],
          avatarUrl: result.data.user.avatar
        })
      }
    }
  })
  return user;
}

/**
 * 根据information获得所需的indents
 */
function getIndentsByInformation(informationId, self){
	wx.request({
		url: config.getIndentsByInformation,
		data: {
			'informationId': informationId
		},
		method: 'GET',
		success: function (result) {
			if (result.data.status == "success") {
				self.setData({
					list: result.data.indents
				})
			}
		}
	})
}

/**
 * 修改订单的状态 同时要修改information
 */
function changeIdent(indentId, status, callback) {
	wx.request({
		url: config.changeIndent,
		data: {
			'indentId': indentId,
			'status': status
		},
		header: {
			'content-type': 'application/x-www-form-urlencoded' // 默认值
		},
		method: 'POST',
		success: function (result) {
			callback()
		}
	})
}

/**
 * 通过用户ID修改用户信息
 */
function editUserInfo(self){
  wx.request({
    url: config.editUserInfo,
    data: {
      'userId': self.data.user.userId,
      'userName': self.data.user.userName,
      'gender': self.data.user.gender,
      'department': self.data.user.department,
      'major': self.data.user.major,
      'password': self.data.user.password, 
      'phoneNum': self.data.user.phoneNum,
      'studentId': self.data.user.studentId,
      'avatar':self.data.user.avatar
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    method: 'POST',
    success: function (result) {

    }
  })
}

/**
 * 通过用户ID获取用户收藏列表
 */
function getCollections(userId,self) {
  var list = [[]]
  wx.request({
    url: config.getCollections,
    data: {
      'userId': userId
    },
    method: 'GET',
    success: function (result) {
      if (result.data.status == "success") {
        list[0] = result.data.collections
        self.setData({
          collections:list[0]
        })
      }
    }
  })
  return list;
}

/**
 * 通过用户ID获取用户评价列表
 */
function getComments(userId,self){
  var list = [[]]
  wx.request({
    url: config.getComments,
    data: {
      'userId': userId
    },
    method: 'GET',
    success: function (result) {
      if (result.data.status == "success") {
        list[0] = result.data.comments
        self.setData({
          comments: list[0]
        })
      }
    }
  })
  return list
}

/**
 * 通过用户ID获取用户投诉列表
 */
function getComplaints(userId,self){
  var list = [[]]
  wx.request({
    url: config.getComplaints,
    data: {
      'userId': userId
    },
    method: 'GET',
    success: function (result) {
      if (result.data.status == "success") {
        list[0] = result.data.complaints
        self.setData({
          complaints:list[0]
        })
      }
    }
  })
  return list;
}

/**
 * 撤销投诉
 */
function cancelComplaint(complaintId){
  wx.request({
    url: config.cancelComplaint,
    data: {
      'complaintId': complaintId
    },
    method: 'GET',
    success: function (result) {
      if (result.data.status == "success") {
        return result.data.complaintId
      }
    }
  })
}

function addComment(indentId, reason, commentType, evaluatorId, callback){
	wx.request({
		url: config.addComment,
		data: {
			'indentId': indentId,
			'reason': reason,
			'commentType': commentType,
			'evaluatorId': evaluatorId
		},
		header: {
			'content-type': 'application/x-www-form-urlencoded' // 默认值
		},
		method: 'POST',
		success: function (result) {
			callback()
		}
	})
	callback()
}

/**
 * 注册用户
 */
function addUser(userName,gender,studentId,password,department,major,phoneNum,avatar,callback){
  var theApp = getApp()
  var list = [[]]
  wx.request({
    url: config.addUser,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    
    data: {
      // 'userId': 0,
      'userName': userName,
      'gender': gender,
      'studentId': studentId,
      'password':password,
      'department': department,
      'major': major,
      // 'wechatId': null,
      'phoneNum': phoneNum,
      // 'favorableNum':0,
      // 'coolects': list,
      'avatar': avatar,
      // 'role':null,
    },
    
    success: function (result) {
      theApp.globalData.userId = result.data.userId
      callback()
    }
  })
}

/**
 * 用户登录
 */
function login(studentId,password,self){
   wx.request({
    url: config.login,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    data:{
    'studentId':studentId,
    'password':password
    },
    
    success: function (result) {
      if (result.data.status == "success") {
        console.log(result.data.loginStatus)
        self.setData({
          status:result.data.loginStatus
        })
      }
    }
  })
}

/**
 * 添加一级标签
 */
function addTag(tagName, description){
  wx.request({
    url: config.addTag,
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    data: {
      'tagName': tagName,
      'description': description
    },

    success: function (result) {
      if (result.data.status == "success") {
      }
    }
  })
}

/**
 * 获取一级标签
 */
function getAllParentTag(self){
  var list = [[]]
  wx.request({
    url: config.getAllParentTag,
    data: {
    },
    method: 'GET',
    success: function (result) {
      if (result.data.status == "success") {
        list = result.data.tags
        self.setData({
          AllParentTag: list
        })
      }
    }
  })
  return list
}

/**
 * 添加二级标签
 */
function addChildTag(tagName, description,parentId) {
  wx.request({
    url: config.addChildTag,
    data: {
      'tagName': tagName,
      'description': description,
      'parentId':parentId
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    method: 'POST',
  })
}

/**
 * 获取二级标签
 */
function getChildTag(parentTagId,self){
  var list = [[]]
  wx.request({
    url: config.getChildTag,
    data: {
      'parentTagId':parentTagId
    },
    method: 'GET',
    success: function (result) {
      if (result.data.status == "success") {
        list = result.data.tags
        self.setData({
          childTag: list
        })
      }
    }
  })
  return list
}

/**
 * 删除标签
 */
function deleteTag(tagId){
  wx.request({
    url: config.deleteTag + '?tagId=' + tagId,
    method: 'DELETE',
    data:{
      'tagId': tagId
    },
    success: function (result) {
    }
  })
}

/**
 * 获取所有投诉
 */
function getAllComplaints(self){
  var list = [[]]
  wx.request({
    url: config.getAllComplaints,
    data: {
    },
    method: 'GET',
    success: function (result) {
      if (result.data.status == "success") {
        list[0] = result.data.complaints
        self.setData({
          AllComplaints: list[0]
        })
      }
    }
  })
  return list
}

/**
 * 管理投诉
 */

function modifyComplaint(complaintId,status){
  wx.request({
    url: config.modifyComplaint,
    data:{
      'complaintId': complaintId,
      'status':status,
    },
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    method: 'POST',
  })

}

/**
 * 格式化时间
 */
function formatTime(time){
  var index = time.indexOf("T");
  var day = time.substring(0, index);
  var time = time.substr(index + 1, 5);
  var showDate = day + " " + time;
  return showDate;
}

module.exports = {
  getAllInformation: getAllInformation,
  addInformation: addInformation,
  removeCollect: removeCollect,  
  addCollect: addCollect,
  addIndent: addIndent,
  addComplaint: addComplaint,
	getComment: getComment,
	getTrades: getTrades,
	getUser: getUser,
	changeIdent: changeIdent,
	deleteIndent: deleteIndent,
	getIndentsByInformation: getIndentsByInformation,
	getCollections: getCollections,
	getComplaints: getComplaints,
  getComments: getComments,
	addComment: addComment,
  cancelComplaint:cancelComplaint,
  editUserInfo:editUserInfo,
  addUser: addUser,
	getUserByStudentId: getUserByStudentId,
  // addParentTag: addParentTag,
  addTag: addTag,
  getAllParentTag: getAllParentTag,
  addChildTag: addChildTag,
  getChildTag: getChildTag,
  deleteTag: deleteTag,
  getAllComplaints: getAllComplaints,
  modifyComplaint:modifyComplaint,
  login:login,
  formatTime:formatTime,
	revokeRelease: revokeRelease
}
